package com.atm.bulksms;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.atm.bulksms.data.BulkSMSData.InboxData;
import com.atm.bulksms.data.BulkSMSData.SMSData;
import com.atm.bulksms.dialog.MessageDetailsDialog;
import com.atm.bulksms.util.Utils;


public class StatusActivity extends ListActivity implements OnItemClickListener{
	
	private static final byte DIALOG_MESSAGE_DETAILS = 0;
	private static final byte DIALOG_PROGRESS_BAR = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inbox);
//		String[] proj = new String[]{InboxData._ID, InboxData.NAME, InboxData.DESCRIPTION, InboxData.START_TIME};
		 Cursor cursor = getContentResolver().query(InboxData.CONTENT_URI, null, null, null, InboxData.START_TIME+" DESC");
		 System.out.println(" No of header item :"+cursor.getCount());
        final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.inbox_item, cursor,
        		new String[] {InboxData.NAME, InboxData.DESCRIPTION}, new int[] {R.id.header_name, R.id.header_desc});
        
        setListAdapter(adapter);
		 cursor.moveToFirst();
		 
        final ListView listView = getListView();

        listView.setItemsCanFocus(false);
        listView.setOnItemClickListener(this);
        
        
      /*  EditText searchView = (EditText)findViewById(R.id.search_view);
        searchView.addTextChangedListener(new TextWatcher(){

			@Override
			public void afterTextChanged(Editable arg0) {
				adapter.changeCursor(getCursor(arg0.toString()));
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				
			}
        	
        });*/
	}

	Cursor messageCursor;
	
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
//		showDialog(DIALOG_PROGRESS_BAR);
		Cursor c = (Cursor)getListAdapter().getItem(position);
		String msg = c.getString(c.getColumnIndex(InboxData.DESCRIPTION));
		long time = c.getLong(c.getColumnIndex(InboxData.START_TIME));
		
		messageCursor = getContentResolver().query(SMSData.CONTENT_URI, null, SMSData.MESSAGE+"=\""+msg+"\" AND "+SMSData.START_TIME+"="+time, null, null);
		System.out.println(" No of messages -- "+messageCursor.getCount());
		StringBuffer displayMessage = new StringBuffer("Message : "+msg);
		/*while(messageCursor.moveToNext()){
			displayMessage.append("\n"+messageCursor.getString(messageCursor.getColumnIndex(SMSData.RECEPIENT)));
		}*/
		System.out.println(displayMessage);
		
		showDialog(DIALOG_MESSAGE_DETAILS);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		Dialog dialog = null;
		switch (id) {
		case DIALOG_MESSAGE_DETAILS:
			dialog = new MessageDetailsDialog(this,messageCursor);
			break;
		case DIALOG_PROGRESS_BAR:
			ProgressDialog progress = new ProgressDialog(this);
			progress.setMessage("Loading ..\n Please wait.");
			dialog = progress;
			break;

		default:
			dialog = super.onCreateDialog(id);
			break;
		}
		return dialog;
	}
	
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
		case DIALOG_MESSAGE_DETAILS:
			((MessageDetailsDialog)dialog).updateMessage(messageCursor);
			break;
		default:
			break;
		}
	}
	

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		Utils.setCursor(messageCursor);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle state) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(state);
		
		messageCursor = Utils.getCursor();
		messageCursor.requery();
	}
	
}

